package src.view;

import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 * <b>Overview</b>
 * This is a splash screen... displays itself for a while and then goes away.
 *
 * <b>Responsibilities</b>
 * Show a splash screen for a little while.  The time it is displayed may be changed.
 *
 * <b>Collaborators</b>
 * None.
 *
 * <b>Implementor</b> - Bruce
 * <b>Tester</b> - Tim
 *
 * @author Bruce
 */
public class Splash extends JFrame
{
	/**
	 * The Serial UID.
	 */
	private static final long serialVersionUID = 3012393670042556692L;

	/**
	 * Creates a new Splash.  This should never be called.
	 */
	public Splash()
	{
		setAlwaysOnTop(true);
		setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
		setResizable(false);
		setUndecorated(true);
	}

	/**
	 * Displays the splash screen for the specified amount of time.
	 *
	 * @param milis the time to display the splash screen for, in miliseconds.
	 */
	public static void show(final long milis)
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			/* (non-Javadoc)
			 * @see java.lang.Runnable#run()
			 */
			public void run()
			{
				Splash a = new Splash();
				a.setVisible(true);
				try
				{
					BufferedImage image = ImageIO.read(new File("images/landshark.png"));
					a.setSize(image.getWidth(), image.getHeight());
					a.getGraphics().setColor(Color.BLACK);
					a.getGraphics().fillRect(0, 0, a.getWidth(), a.getHeight());
					a.getGraphics().drawImage(image, 0, 0, null);
				}
				catch(Exception e){e.printStackTrace();}

				Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
				a.setLocation((int)(p.getX() - a.getSize().getWidth() / 2), (int)(p.getY() - a.getSize().getHeight() / 2));
				long time = System.currentTimeMillis() + milis;
				while(System.currentTimeMillis() < time)
				{a.paint(a.getGraphics());}
				a.dispose();
			}
		});
	}
}